package com.china08.yxyapi.api.manage.tec.teaching.resource.myCollect;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.FloatUtil;
import com.china08.yxyapi.util.YxyResUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class MyCollectDetailController extends YxyApiBaseController {

    @Autowired
    private YxyResUtil YxyResUtil;

    @Autowired
    private YxyResourceService yrs;

    @Autowired
    private YxyResResourceRepository yrrr;

    @ApiOperation(value = "老师收藏资源详情")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/resource/myCollect/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws IllegalAccessException, InvocationTargetException {
        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        RespModel model = new RespModel();
        ElasticYxyResResource elasticYxyResResource = yrs.findById(reqModel.id);
        Assert.notNull(elasticYxyResResource, "找不到资源，请刷新页面");
        YxyResResource resource = yrrr.findById(reqModel.id);
        Assert.notNull(resource, "找不到资源，请刷新页面");

        if (resource.getIsDelete() == 1) {
            model.setScan(false);
            return model;
        }

        resource.setPv(resource.getPv() + 1);
        yrrr.save(resource);

        // 是否已收藏
        BeanUtils.copyProperties(model, elasticYxyResResource);
        if (StringUtils.isNotBlank(userId)) {
            String role = getProfile(request, response).getRoleId();
            if (StringUtils.equalsIgnoreCase(role, "ROLE_TEACHER")) {
                long isCheck = yrs.checkFavor(userId, schoolId, "ROLE_TEACHER", reqModel.id);
                model.setIsFavor(isCheck > 0 ? 1 : 0);
            }
        }
        if (elasticYxyResResource.getUploadTime() != null) {
            model.uploadTimeFormat = DateToolUtils.format(elasticYxyResResource.getUploadTime(),
                    DateToolUtils.FORMAT_SHORT_YMD);
        }
        model.setMessage("成功");
        // 资源下载
        String download = YxyResUtil.getDownloadUrl(reqModel.id);
        model.setDownloadLocation(download);
        model.setPreview(YxyResUtil.getPreviewUrl(reqModel.id));
        model.setBookContentNames(elasticYxyResResource.getBookContentNameList());
        model.setMark(FloatUtil.dot1(elasticYxyResResource.getMark()));
        model.setExtion(elasticYxyResResource.getExtension());
        model.setIsVip(elasticYxyResResource.getIsVip());
        model.setIsHighQualityYx(elasticYxyResResource.getIsHighQualityYx());
        model.setResVip(Boolean.valueOf(getProfile(request, response).getResVip()));
        model.setRoleId(getProfile(request, response).getRoleId());
        EsResourcePvUpdateUtil.updateEsResoure(reqModel.id + "_1");
        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请指定资源");
        }

    }

    public static class RespModel extends YxyApiRespModel {
        // id
        public String id;
        // 标题
        public String title;

        // 类型
        public String typeName;

        // 上传人名
        public String uploaderName;

        // 上传者学校明天
        public String uploaderSchoolName;

        // 审核人
        public String auditUserName;

        // 审核学校
        public String auditSchoolName;

        // 资源简介
        public String intro;

        // 文件的状态信息 0初始上传， 1 转码中 ，2 转码成功 ，3 转码失败
        public Integer convertStatus = 0;

        /**
         * 文件路径
         */
        public String location;

        // 评分
        public float mark = 0f;

        public String formatId;

        public String formatName;

        // 是否收藏 0否 1是
        public Integer isFavor = 0;

        // 上传时间
        public String uploadTimeFormat = "";

        // 所属目录名称
        public List<String> bookContentNames;

        public String downloadLocation = "";

        // 预览
        public String preview = "";

        // 审核状态 0：待审核 1：已通过 2：未通过 3：已下架
        public Integer status = 0;

        public String extion;

        public Boolean isVip = false;

        public Integer isHighQualityYx = 0;

        public Boolean resVip = false;

        public String roleId;

        public Boolean scan = true;

        public Boolean getScan() {
            return scan;
        }

        public void setScan(Boolean scan) {
            this.scan = scan;
        }

        public Boolean getResVip() {
            return resVip;
        }

        public void setResVip(Boolean resVip) {
            this.resVip = resVip;
        }

        public String getRoleId() {
            return roleId;
        }

        public void setRoleId(String roleId) {
            this.roleId = roleId;
        }

        public Boolean getIsVip() {
            return isVip;
        }

        public void setIsVip(Boolean isVip) {
            this.isVip = isVip;
        }

        public Integer getIsHighQualityYx() {
            return isHighQualityYx;
        }

        public void setIsHighQualityYx(Integer isHighQualityYx) {
            this.isHighQualityYx = isHighQualityYx;
        }

        public String getExtion() {
            return extion;
        }

        public void setExtion(String extion) {
            this.extion = extion;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getTypeName() {
            return typeName;
        }

        public void setTypeName(String typeName) {
            this.typeName = typeName;
        }

        public String getUploaderName() {
            return uploaderName;
        }

        public void setUploaderName(String uploaderName) {
            this.uploaderName = uploaderName;
        }

        public String getUploaderSchoolName() {
            return uploaderSchoolName;
        }

        public void setUploaderSchoolName(String uploaderSchoolName) {
            this.uploaderSchoolName = uploaderSchoolName;
        }

        public String getAuditUserName() {
            return auditUserName;
        }

        public void setAuditUserName(String auditUserName) {
            this.auditUserName = auditUserName;
        }

        public String getAuditSchoolName() {
            return auditSchoolName;
        }

        public void setAuditSchoolName(String auditSchoolName) {
            this.auditSchoolName = auditSchoolName;
        }

        public String getIntro() {
            return intro;
        }

        public void setIntro(String intro) {
            this.intro = intro;
        }

        public Integer getConvertStatus() {
            return convertStatus;
        }

        public void setConvertStatus(Integer convertStatus) {
            this.convertStatus = convertStatus;
        }

        public String getLocation() {
            return location;
        }

        public void setLocation(String location) {
            this.location = location;
        }

        public float getMark() {
            return mark;
        }

        public void setMark(float mark) {
            this.mark = mark;
        }

        public String getFormatId() {
            return formatId;
        }

        public void setFormatId(String formatId) {
            this.formatId = formatId;
        }

        public String getFormatName() {
            return formatName;
        }

        public void setFormatName(String formatName) {
            this.formatName = formatName;
        }

        public Integer getIsFavor() {
            return isFavor;
        }

        public void setIsFavor(Integer isFavor) {
            this.isFavor = isFavor;
        }

        public String getUploadTimeFormat() {
            return uploadTimeFormat;
        }

        public void setUploadTimeFormat(String uploadTimeFormat) {
            this.uploadTimeFormat = uploadTimeFormat;
        }

        public List<String> getBookContentNames() {
            return bookContentNames;
        }

        public void setBookContentNames(List<String> bookContentNames) {
            this.bookContentNames = bookContentNames;
        }

        public String getDownloadLocation() {
            return downloadLocation;
        }

        public void setDownloadLocation(String downloadLocation) {
            this.downloadLocation = downloadLocation;
        }

        public String getPreview() {
            return preview;
        }

        public void setPreview(String preview) {
            this.preview = preview;
        }

        public Integer getStatus() {
            return status;
        }

        public void setStatus(Integer status) {
            this.status = status;
        }

    }
}
